package ai.leinao.alarm;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import lombok.Data;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * description
 *
 * @author zhong.wang
 * @date 2025/10/16
 **/
@Data
public class When {
    /**
     * 1-水平超限('>','<','>=' '<=','>') 2-区间告警(1<x<=2, 1<=x<=2, 1<x<2,1<=x<=2) 3-状态告警（ '==' '!='）
     */
    String type;

    /**
     * in-窗内 out-窗外
     */
    String betweenType;

    /**
     * 操作符
     */
    String operator;

    /**
     * 条件(&& ||)
     */
    String condition;

    /**
     * 字段
     */
    String field;

    /**
     * 值
     */
    String value;

    /**
     * 如果是区间告警，对应得数据在这里面
     */
    List<When> whens;

    public static void main(String[] args) {
        // x>1
        When when = new When();
        when.setType("1");
        when.setOperator(">");
        when.setField("x");
        when.setValue("1");
        when.setCondition("&&");
        when.setWhens(null);
        System.out.println(JSONUtil.toJsonStr(when));
        System.out.println(getExpression(when));
        // x<=1
        When when2 = new When();
        when2.setType("1");
        when2.setOperator("<=");
        when2.setField("x");
        when2.setValue("1");
        when2.setCondition("&&");
        when2.setWhens(null);
        System.out.println(JSONUtil.toJsonStr(when2));
        System.out.println(getExpression(when2));

        // x==1
        When when3 = new When();
        when3.setType("3");
        when3.setOperator("==");
        when3.setField("x");
        when3.setValue("1");
        when3.setCondition("&&");
        when3.setWhens(null);
        System.out.println(JSONUtil.toJsonStr(when3));
        System.out.println(getExpression(when3));

        // x!=1
        When when4 = new When();
        when4.setType("3");
        when4.setOperator("!=");
        when4.setField("x");
        when4.setValue("1");
        when4.setCondition("&&");
        when4.setWhens(null);
        System.out.println(JSONUtil.toJsonStr(when4));
        System.out.println(getExpression(when4));

        // x>1 && x<=2
        When when5 = new When();
        when5.setType("2");
        when5.setBetweenType("in");
        when5.setCondition("&&");
        When when5_1 = new When();
        when5_1.setOperator(">");
        when5_1.setValue("1");
        when5_1.setField("x");
        When when5_2 = new When();
        when5_2.setOperator("<=");
        when5_2.setValue("2");
        when5_2.setField("x");
        when5.setWhens(Arrays.asList(when5_1, when5_2));
        System.out.println(JSONUtil.toJsonStr(when5));
        System.out.println(getExpression(when5));


        // x<1 || x>2
        When when6 = new When();
        when6.setType("2");
        when6.setBetweenType("out");
        when6.setCondition("&&");
        When when6_1 = new When();
        when6_1.setOperator("<");
        when6_1.setValue("1");
        when6_1.setField("x");
        When when6_2 = new When();
        when6_2.setOperator(">");
        when6_2.setValue("2");
        when6_2.setField("x");
        when6.setWhens(Arrays.asList(when6_1, when6_2));
        System.out.println(JSONUtil.toJsonStr(when6));
        System.out.println(getExpression(when6));

        // x>1
        When when7 = new When();
        when7.setType(null);
        when7.setOperator("&&");
        When when7_1 = new When();
        when7_1.setOperator(">");
        when7_1.setValue("1");
        when7_1.setField("x");
        when7.setWhens(Arrays.asList(when7_1));

        // x>1 && y<=2 && z>3
        When when8 = new When();
        when8.setType(null);
        when8.setCondition("&&");
        When when8_1 = new When();
        when8_1.setOperator(">");
        when8_1.setValue("1");
        when8_1.setField("x");
        When when8_2 = new When();
        when8_2.setOperator("<=");
        when8_2.setValue("2");
        when8_2.setField("y");
        When when8_3 = new When();
        when8_3.setOperator(">");
        when8_3.setValue("3");
        when8_3.setField("z");
        when8.setWhens(Arrays.asList(when8_1, when8_2, when8_3));
        System.out.println(JSONUtil.toJsonStr(when8));
        System.out.println(getExpression(when8));
    }


    public static String getExpression(When when) {
        if(Objects.isNull(when.type)){
            if(CollUtil.isNotEmpty(when.whens)){
                List<String> childExpressions = when.whens.stream()
                        .map(c -> c.field + " " + c.operator + " " + c.value)
                        .collect(Collectors.toList());
                return String.join(" " + when.condition + " ", childExpressions);
            }
        }else{
            switch (when.type) {
                case "1": // 水平超限
                    return when.field + " " + when.operator + " " + when.value;
                case "2": // 区间告警
                    if (when.whens != null && when.whens.size() == 2) {
                        When when1 = when.whens.get(0);
                        When when2 = when.whens.get(1);
                        if ("in".equals(when.betweenType)) {
                            return when1.field + " " + when1.operator + " " + when1.value + " "
                                    + when.getCondition() + " "
                                    + when2.field + " " + when2.operator + " " + when2.value;
                        } else { // out
                            return "!(" + when1.field + " " + when1.operator + " " + when1.value + " "
                                    + when.getCondition() + " "
                                    + when2.field + " " + when2.operator + " " + when2.value + ")";
                        }
                    }
                    return "";
                case "3": // 状态告警
                    return when.field + " " + when.operator + " " + when.value;
                default: //组合告警
                    return "";
            }
        }
        return "";
    }

}

